package com.leetcode.May;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

/**
 * @Description: 三数之和
 * @Author: Mr.Fxy
 * @CreateTime: 2024-05-13 18:16
 */
public class SumOfThree {
    public static void main(String[] args) {
        int[] cs=new int[]{0,0,0,0};
        List<List<Integer>> lists = threeSum(cs);
    }

    public static List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> millionYuanList = new ArrayList<>();
        if (nums.length < 3) {
            return millionYuanList;
        }
        Arrays.sort(nums);
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] > 0) break;
            int first = nums[i];
            if (i > 0 && nums[i] == nums[i - 1]) continue;
            HashSet<Integer> set = new HashSet<>();
            for (int j = i + 1; j < nums.length; j++) {
                int third = nums[j];
                int second = -(third + first);
                if (set.contains(second)) {
                    millionYuanList.add(Arrays.asList(first, second, third));
                    while(j < nums.length - 1 && nums[j] == nums[j + 1]) j++;
                }
                set.add(third);
            }
        }
        return millionYuanList;
    }
}
